<template>
  <el-card style="margin-top: 20px">
    <template #header>
      <div class="card-header">
        <el-button
          type="primary"
          size:default
          :icon="Plus"
          @click="showAddTrademark"
          >添加</el-button
        >
      </div>
    </template>
    <!-- html中表格按照行走，在element-plus中表格按照列去走 -->
    <!-- el-table展示数据 -->
    <!-- border带边框  stripe斑马条-->
    <!-- el-table-column 代表表格的每一列 -->
    <!-- align对齐方式center 居中  -->
    <!-- data及时要渲染的数据 -->
    <el-table :data="trademarkList" border stripe style="margin-bottom: 20px">
      <el-table-column
        prop="date"
        label="序号"
        type="index"
        width="80"
        align="center"
      />
      <!-- 列暗含遍历 -->
      <!-- prop写需要渲染的属性 -->
      <el-table-column prop="tmName" label="品牌名称" />
      <el-table-column label="品牌logo">
        <template v-slot="{ row, $index }">
          <el-image
            :src="row.logoUrl"
            style="width: 90px; height: 50px"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" width="200">
        <template v-slot="{ row, $index }">
          <el-button type="warning" size="small" :icon="Edit" @click=""
            >修改</el-button
          >
          <el-button type="danger" size="small" :icon="Delete" @click=""
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- v-model:current-page="currentPage4"
      v-model:page-size="pageSize4" -->
    <el-pagination
      :page-sizes="[3, 5, 7, 9]"
      :total="total"
      layout="  prev, pager, next, jumper, ->,sizes,total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>

  <!--dialogFormVisible在控制对话框的显示和隐藏  title标题-->
  <el-dialog v-model="dialogFormVisible" title="添加品牌">
    <!-- table展示数据 form收集数据 -->
    <el-form style="width: 80%">
      <!-- lable 表单项的名称  label-width文本宽度-->
      <el-form-item label="品牌名称" label-width="100px">
        <el-input />
      </el-form-item>
      <el-form-item label="品牌Logo" label-width="100px">
        <el-upload
          class="avatar-uploader"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />

          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          <template #tip>
            <div class="el-upload__tip">上传的图片必须是jpg且不大于2M</div>
          </template>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Trademark",
});
</script>

<script lang="ts" setup>
import { Delete, Edit, Plus } from "@element-plus/icons-vue";
import { ref, onMounted } from "vue";
import trademarkApi from "@/api/trademark";
import type { TrademarkDate } from "@/api/trademark";
import { ElMessage } from "element-plus";
import type { UploadProps } from "element-plus";
const page = ref(1); //当前页码
const limit = ref(3); //每页大小
const total = ref(0); //总数
const trademarkList = ref(<TrademarkDate[]>[]);
const dialogFormVisible = ref(false);

//发送获取列表品牌的参数
async function getPageTmList() {
  try {
    const res = await trademarkApi.getpageList(page.value, limit.value);
    // console.log(res);
    total.value = res.total;

    trademarkList.value = res.records;
  } catch (error) {
    ElMessage.error("获取品牌列表失败");
  }
}
onMounted(() => {
  getPageTmList();
});
//每页大小自动执行的回调函数
function handleSizeChange(num: number) {
  //形参接收最新的默认页页面大小
  limit.value = num;
  getPageTmList();
}
//当前页码变化执行的回调函数
function handleCurrentChange(num: number) {
  //形参接收最新当前页码
  page.value = num;
  getPageTmList();
}
//点击添加更改显示状态
function showAddTrademark() {
  dialogFormVisible.value = true;
}

const imageUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!);
};

const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  if (rawFile.type !== "image/jpeg") {
    ElMessage.error("Avatar picture must be JPG format!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("Avatar picture size can not exceed 2MB!");
    return false;
  }
  return true;
};
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
